package com.hitplane.core 
{
	import com.hitplane.debug.DebugTrace;
	import com.hitplane.elements.state.BulletHitState;
	import com.hitplane.elements.state.BulletNormalState;
	import com.hitplane.interfaces.IBullet;
	import com.hitplane.interfaces.IState;
	
	import flash.events.Event;
	
	/**
	 * @describe  	子弹类
	 * @author  	rrDog
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		... 
	 */
	public class Bullet extends ItemBaseDisplay implements IBullet
	{
		private var _vx:Number;
		private var _vy:Number;
		private var _harm:Number;
		/* INTERFACE com.hitplane.interfaces.IBullet */
		public function get vx():Number {return _vx;}
		public function set vx(num:Number):void {_vx = num;}
		public function get vy():Number {return _vy;}
		public function set vy(num:Number):void {_vy = num;}
		public function get harm():Number { return _harm; }
		public function set harm(value:Number):void {_harm = value;}
		
		public var bulletNormalState:IState;
		public var bulletHitState:IState;
		//public =============================================
		//override=====================================================
		override protected function addEvent():void{
			//碰撞动画完成之后，释放内存
			this.addEventListener(BulletHitState(bulletHitState).ANIMAL_END,onBulletHit);
		}
		override protected function removeEvent():void{
			this.removeEventListener(BulletHitState(bulletHitState).ANIMAL_END,onBulletHit);
		}
		override protected function initState():void{
			bulletNormalState=new BulletNormalState(this);
			bulletHitState=new BulletHitState(this);
		}
		override public function onTick():void {
			this.x+=vx;
			this.y+=vy;
			//超出场景范围则移除
			var buffer:int=10;
			var leftSide:int = -1 * buffer;
			var rightSide:int = stage.stageWidth + buffer;
			var upSide:int = -1 * buffer;
			var downSide:int = stage.stageHeight + buffer;
			
			if (x < leftSide || x > rightSide || y < upSide || y > downSide) {
				dispose();
				removeFromManager();
//				DebugTrace.traceObj(this,"bullet out to stage.");
			}
		}
		override public function hasCollision():void 
		{
			setState(bulletHitState);
			removeFromManager();
		}
		//private=============================================
		private function onBulletHit(e:Event):void{
			dispose();
		}
	}
	
}